package org.ulht.lei.redes.redes;
import java.net.*;
import java.io.*;
// Aguarda comunicacao na porta 6500,
// recebe mensagens e devolve-as
public class EchoServerThread {
	public static void main(String args[]) throws Exception {//inicio main		
		//criar socket na porta 6500
		ServerSocket server = new ServerSocket(6500);
		System.out.println ("servidor iniciado na porta 6500");		
		while (true){//aguarda clientes
			Socket socket = null;
			socket = server.accept();
			System.out.println("nova conexao..");
			Thread t = new Thread(new EchoClientThread(socket));                             
			t.start();			
		}//fim aguarda clientes 		
	}//fim main	

	public static class EchoClientThread implements Runnable{
		private Socket s;
		public EchoClientThread(Socket socket) {
			this.s = socket;
		}
		public void run() {	
			String threadName = Thread.currentThread().getName();//nome da thread
			String stringClient = s.getInetAddress().toString();//IP do cliente			
			System.out.println("conectado com " + stringClient);		
			try{//inicio try				
				BufferedReader input = new BufferedReader(
						new InputStreamReader(s.getInputStream()));			
				PrintStream output = new PrintStream(
						s.getOutputStream(),true);
				String line;
				while ((line = input.readLine()) !=null) {//ciclo de input				
					System.out.println (stringClient+": "+threadName+": "+line);//imprime mensagem
					if (line.equalsIgnoreCase("quit")){
						break;
					}						
					output.println(line); // echo do input para output						
				}//fim do ciclo de input 
				output.println("ate logo!"); 
				input.close();//termina input 
				output.close();//termina output
				s.close();//termina socket							
			}//fim try
			catch (Exception e){
				System.err.println("Erro: "+e);
			}
			System.out.println("cliente "+ stringClient+" desconectado!");					
		}//fim metodo run  
	}//fim classe EchoClientThread
}//fim classe EchoServerThread 


